package com.gz.jdbc;

import org.junit.jupiter.api.Test;

import java.io.FileInputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

public class Jdbc02 {

   @Test//方式一
    public void connect01() throws SQLException {
       Driver driver = new com.mysql.cj.jdbc.Driver();//创建driver对象
       String url = "jdbc:mysql://localhost:3306/a0327";
       String username = "root";
       String password = "123456";

       Properties properties = new Properties();
       properties.setProperty("user", "root");
       properties.setProperty("password", "123456");
       Connection conn = driver.connect(url, properties);
       System.out.println("方式1= "+conn);
    }

    @Test//方式二,使用反射加载Driver类，动态加载，更加的灵活，减少依赖性
    public void connect02() throws SQLException, ClassNotFoundException, InstantiationException, IllegalAccessException {
        Class<?> aClass = Class.forName("com.mysql.cj.jdbc.Driver");
        Driver driver = (Driver)aClass.newInstance();
        String url = "jdbc:mysql://localhost:3306/a0327";
        String username = "root";
        String password = "123456";
        Properties properties = new Properties();
        properties.setProperty("user", "root");
        properties.setProperty("password", "123456");
        Connection conn = driver.connect(url, properties);
        System.out.println("方式2= "+conn);
    }

    @Test//方式三
    public void connect03() throws SQLException, ClassNotFoundException, InstantiationException, IllegalAccessException {
        Class<?> aClass = Class.forName("com.mysql.cj.jdbc.Driver");
        Driver driver = (Driver)aClass.newInstance();
        String url = "jdbc:mysql://localhost:3306/a0327";
        String username = "root";
        String password = "123456";
        Properties properties = new Properties();
        properties.setProperty("user", "root");
        properties.setProperty("password", "123456");
        DriverManager.registerDriver(driver);//注册驱动
        Connection connection = DriverManager.getConnection(url, username, password);
        System.out.println("方式3= "+connection);
    }
    @Test//方式四，工作中使用最多，推荐使用
    public void connect04() throws SQLException, ClassNotFoundException, InstantiationException, IllegalAccessException {
       //使用反射加载Driver类
        //在加载Driver类时完成注册，因为是Static代码块
        Class.forName("com.mysql.cj.jdbc.Driver");
        //底层源码
        /*public Driver() throws SQLException {
        }

        static {
            try {
                DriverManager.registerDriver(new com.mysql.cj.jdbc.Driver());
            } catch (SQLException var1) {
                throw new RuntimeException("Can't register driver!");
            }
        }*/
       String url = "jdbc:mysql://localhost:3306/a0327";
       String username = "root";
       String password = "123456";
       Connection connection = DriverManager.getConnection(url, username, password);
        System.out.println("方式4= "+connection);


    }
    @Test//方式五,在方式四的基础上添加配制文件，更加灵活
    public void connect05() throws SQLException, ClassNotFoundException, InstantiationException, IllegalAccessException, IOException {
        Properties properties = new Properties();
        properties.load(new FileInputStream("src\\mysql.properties"));
        //获取相关值
        String url = properties.getProperty("url");
        String user = properties.getProperty("user");
        String password = properties.getProperty("password");
        String driver = properties.getProperty("driver");
        Class.forName(driver);//可以不写，但还是建议写，因为有可能连接的是其他数据库
        Connection connection = DriverManager.getConnection(url, user, password);
        System.out.println("方式5: "+connection);

    }
}
